/**
 * 
 */
package com.taobao.snowsnow.util;

import java.util.Date;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.StringFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.taobao.snowsnow.hibernate.pojo.Shopper;

/**
 * @author Tux
 *
 * 创建时间: 2011-6-14 下午06:28:19
 */
public class FetchMyleftInfo {
	private final static String URL = "http://rate.taobao.com/user-rate-8f5c8be3cddc8260b7116aa0435d9151.htm?spm=a1z10.1.0.41.4miJUt";
	private final static String FURL = "http://favorite.taobao.com/collect_item_relation---59951133-0-.htm";
	
	private Shopper shopper;
	private Parser p;
	
	public FetchMyleftInfo() {
		shopper = new Shopper();
		try {
			p = new Parser(URL);
			
			getTitle();
			getQuantity();
			getCredit();
			getFavor();
			getDate();
		} catch (ParserException e) {
			e.printStackTrace();
		}
	}
	
	public void getTitle() throws ParserException {
		NodeFilter t_name = new HasAttributeFilter("class", "title");
		getShopper().setName(filterHtml(p, t_name).elementAt(0).toPlainTextString().trim());
		p.reset();
	}
	
	public void getQuantity() throws NumberFormatException, ParserException {
		NodeFilter quantity = new StringFilter("宝贝数量");			
		getShopper().setQuantity(Integer.valueOf(filterHtml(p, quantity).elementAt(0).getParent().getNextSibling().toPlainTextString().trim()));
		p.reset();
	}
	
	public void getCredit() throws NumberFormatException, ParserException {
		NodeFilter credit = new StringFilter("卖家信用");
		String s = filterHtml(p, credit).elementAt(2).getText().trim();
		String[] ss = encode(s).split(":");
		getShopper().setCredit(Integer.valueOf(ss[1]));
		p.reset();
	}
	
	public void getFavor() throws ParserException {
		Parser p = new Parser(FURL);
		
		NodeList list = new NodeList();
		NodeFilter STRONG = new TagNameFilter("STRONG");
		list = filterHtml(p, STRONG);
		getShopper().setFavour(Integer.valueOf(list.elementAt(1).getNextSibling().toPlainTextString().trim()));
	}
	
	public void getDate() {
		shopper.setDate(Util.dateToString(new Date()));
	}
	
	// 过滤某些字符,例如: \n \t
	public String encode(String s) {
		if (s == null)
			return null;
		StringBuffer sb = new StringBuffer();
		int n = s.length();
		for (int i = 0; i < n; i++) {
			int c = s.codePointAt(i);
			switch (c) {
			case 9:
			case 10:
			case 13:
			case 32:
				break;
			case 65306:
				sb.append(":");
				break;
			default:
				sb.append(s.charAt(i));
			}
		}
		return sb.toString();
	}

	public NodeList filterHtml(Parser p, NodeFilter filter)	throws ParserException {
		NodeList list = new NodeList();
		
		for (NodeIterator e = p.elements(); e.hasMoreNodes();)
			e.nextNode().collectInto(list, filter);
		return list;
	}

	public void setShopper(Shopper shopper) {
		this.shopper = shopper;
	}

	public Shopper getShopper() {
		return shopper;
	}
}
